package com.dy.lock.screen.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import com.dy.lock.screen.dao.ChatDao;
import com.dy.lock.screen.pojo.ChatInfo;
import com.dy.lock.screen.scoket.Head;
import com.dy.lock.screen.service.ChatService;

@Service("chatService")
public class ChatServiceImpl implements ChatService {

	@Resource(name = "chatDao")
	private ChatDao chatDao;

	@Override
	public int save(ChatInfo chatInfo, Head head) {
		String id = "";
		String maxId = this.chatDao.queryMaxId();
		if (StringUtils.isBlank(maxId)) {
			id = "10000000";
		} else {
			id = String.valueOf(Integer.parseInt(maxId) + 1);
		}
		chatInfo.setId(id);
		if (StringUtils.isBlank(chatInfo.getChatPicPath())) {
			chatInfo.setChatPicPath("");
		}
		if (StringUtils.isBlank(chatInfo.getChatContent())) {
			chatInfo.setChatContent("");
		}
		return this.chatDao.insert(chatInfo);
	}

	@Override
	public List<ChatInfo> getLastChatInfo(String goodsId) {
		return this.chatDao.queryLastByGoodsId(goodsId);
	}

	@Override
	public List<ChatInfo> getAllChatInfo(String goodsId) {
		return this.chatDao.queryAllChatByGoodsId(goodsId);
	}

	@Override
	public Integer queryCountByGoodsId(String goodsId, Head head) {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("goodsId", goodsId);
		return this.chatDao.queryCount(map);
	}

}
